package org.microsites.security;

import java.lang.reflect.Method;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;

public class StacktraceAdvisor extends StaticMethodMatcherPointcutAdvisor implements MethodInterceptor {

	private static final long serialVersionUID = -2745083830776467502L;

	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		String name = mi.getMethod().getDeclaringClass().getName() + ':' + mi.getMethod().getName();
		try {
			System.out.println(name + " -- start");
			return mi.proceed();
		} catch (Throwable ex) {
			ex.printStackTrace();
			throw ex;
		} finally {
			System.out.println(name + " -- end");
		}
	}

	@Override
    @SuppressWarnings("rawtypes")
	public boolean matches(Method method, Class cls) {
		return method.getDeclaringClass().getName().startsWith("org.microsites.service.");
	}

	@Override
	public Advice getAdvice() {
		return this;
	}
	
}
